package com.woniu.adapter;

import com.woniu.outlet.dao.CompanyVipApplicationMapper;
import com.woniu.outlet.dao.PotentialMessageMapper;
import com.woniu.outlet.po.CompanyVipApplication;
import com.woniu.outlet.po.CompanyVipApplicationExample;
import com.woniu.outlet.po.PotentialMessage;
import com.woniu.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Component
@Transactional
public class CompanyVipApplicationAdapter {

    @Autowired
    private CompanyVipApplicationMapper companyVipApplicationMapper;

    @Autowired
    private PotentialMessageMapper potentialMessageMapper;

    //客户经理提出将某个客户申请为VIP，然后需要营销经理来审批、审批通过了，就给客户发一条消息！！
    public int applicationvip(CompanyVipApplication companyVipApplication){
        int insert = companyVipApplicationMapper.insert(companyVipApplication);
        return insert;
    }

    //营销经理可以查看所有没有处理的申请
    public List<CompanyVipApplication> findAllCompanyVipApplication(){
        CompanyVipApplicationExample example = new CompanyVipApplicationExample();
        CompanyVipApplicationExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyvipapplicationStatusEqualTo(0);
        List<CompanyVipApplication> companyVipApplications = companyVipApplicationMapper.selectByExample(example);
        return companyVipApplications;
    }

    //营销经理拒绝此客户成为vip客户
    public int confusevipapplication(Integer companyvipapplicationId){
        int confusevipapplication = companyVipApplicationMapper.confusevipapplication(companyvipapplicationId);
        return confusevipapplication;
    }


    //营销经理同意此客户成为vip客户就给客户发一条消息
    public int agreevipapplication(Integer companyid,Integer companyvipapplicationId){
        //首先处理申请表，修改申请表的状态
        int agreevipapplication = companyVipApplicationMapper.agreevipapplication(companyvipapplicationId);

        //处理企业客户表的状态，设为vip客户
        int isvip = companyVipApplicationMapper.isvip(companyid);
        //然后到消息队列里面发消息
        PotentialMessage potentialMessage = new PotentialMessage();
        potentialMessage.setPotentialmessageExchange("potentialcustomer_exchange");
        potentialMessage.setPotentialmessageKey("potentialcustomer.xxx");
        potentialMessage.setPotentialmessageCount(10);
        potentialMessage.setPotentialmessageContext(""+companyid+","+"agreevipapplication");
        potentialMessage.setPotentialmessageStatus(0);
        //将这个message存到数据库
        potentialMessageMapper.insert(potentialMessage);
        return 0;
    }





}
